package com.fulu.game.bi.service.impl.dispatch;


import cn.hutool.core.date.DateUtil;
import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.common.util.math.MathUtils;
import com.fulu.game.bi.entity.bo.channel.ExposurePlayeeOverviewListBO;
import com.fulu.game.bi.entity.dto.ExposureDynamicListDTO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.dispatch.DynamicExposureRealtimeVO;
import com.fulu.game.bi.entity.vo.dispatch.ExposureDynamicListVO;
import com.fulu.game.bi.hbase.HBaseDynamicExposureStatDAO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DynamicExposureStatServiceImpl {
    @Autowired
    private HBaseDynamicExposureStatDAO hBaseDynamicExposureStatDAO;

    public static final Map<Integer, String> exposureDynamicStatMap;


    static {
        exposureDynamicStatMap = new HashMap<>();
        exposureDynamicStatMap.put(QueryTypeStatusEnum.DAY.getType(), "exposure_dynamic_stat_day");
        exposureDynamicStatMap.put(QueryTypeStatusEnum.WEEK.getType(), "exposure_dynamic_stat_week");
        exposureDynamicStatMap.put(QueryTypeStatusEnum.MONTH.getType(), "exposure_dynamic_stat_month");
    }

    public List<DynamicExposureRealtimeVO> realtime() {
        DynamicExposureRealtimeVO today = hBaseDynamicExposureStatDAO.findByParameter(DateUtil.today());
        today = today == null ? new DynamicExposureRealtimeVO() : today;
        DynamicExposureRealtimeVO yesterday = hBaseDynamicExposureStatDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -1)));
        yesterday = yesterday == null ? new DynamicExposureRealtimeVO() : yesterday;
        DynamicExposureRealtimeVO theDayBeforeYesterday = hBaseDynamicExposureStatDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -2)));
        theDayBeforeYesterday = theDayBeforeYesterday == null ? new DynamicExposureRealtimeVO() : theDayBeforeYesterday;
        DynamicExposureRealtimeVO lastWeekYesterdaySameDay = hBaseDynamicExposureStatDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -8)));
        lastWeekYesterdaySameDay = lastWeekYesterdaySameDay == null ? new DynamicExposureRealtimeVO() : lastWeekYesterdaySameDay;
        List<DynamicExposureRealtimeVO> list = new ArrayList<>();
        yesterday.setExposureUserNumDay(MathUtils.chainRatio(yesterday.getExposureUserNum(), theDayBeforeYesterday.getExposureUserNum()));
        yesterday.setExposureUserNumWeek(MathUtils.chainRatio(yesterday.getExposureUserNum(), lastWeekYesterdaySameDay.getExposureUserNum()));
        yesterday.setExposureNumDay(MathUtils.chainRatio(yesterday.getExposureNum(), theDayBeforeYesterday.getExposureNum()));
        yesterday.setExposureNumWeek(MathUtils.chainRatio(yesterday.getExposureNum(), lastWeekYesterdaySameDay.getExposureNum()));
        yesterday.setViewUserNumDay(MathUtils.chainRatio(yesterday.getViewUserNum(), theDayBeforeYesterday.getViewUserNum()));
        yesterday.setViewUserNumWeek(MathUtils.chainRatio(yesterday.getViewUserNum(), lastWeekYesterdaySameDay.getViewUserNum()));
        yesterday.setViewNumDay(MathUtils.chainRatio(yesterday.getViewNumDay(), theDayBeforeYesterday.getViewNumDay()));
        yesterday.setViewNumWeek(MathUtils.chainRatio(yesterday.getViewNumDay(), lastWeekYesterdaySameDay.getViewNumDay()));
        yesterday.setCtrDay(MathUtils.chainRatio(yesterday.getCtr(), theDayBeforeYesterday.getCtr()));
        yesterday.setCtrWeek(MathUtils.chainRatio(yesterday.getCtr(), lastWeekYesterdaySameDay.getCtr()));

        list.add(today);
        list.add(yesterday);
        return list;
    }

    public List<ExposureDynamicListVO> list(ExposureDynamicListDTO dto) {
        ExposurePlayeeOverviewListBO bo = new ExposurePlayeeOverviewListBO();
        BeanUtils.copyProperties(dto, bo);
        bo.setTableName(exposureDynamicStatMap.get(dto.getQueryType()));
        List<ExposureDynamicListVO> list = hBaseDynamicExposureStatDAO.list(bo);
        list.forEach(map -> map.setQueryType(dto.getQueryType()));
        return list;
    }

    public void export(ExposureDynamicListDTO dto) {
        String fileName = "动态曝光总览";
        LinkedHashMap<String, Mapper<ExposureDynamicListVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(dto.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("曝光人数", v -> v.getExposureUserNum() + "");
        map.put("曝光次数", v -> v.getExposureNum() + "");
        map.put("动态详情浏览人数", v -> v.getViewUserNum() + "");
        map.put("动态详情浏览次数", v -> v.getViewNum() + "");
        map.put("CTR", v -> v.getCtr() + "");
        Querier querier = (num) -> {
            PageInfo<ExposureDynamicListVO> pageInfo = new PageInfo<>(list(dto));
            pageInfo.setHasNextPage(false);
            return pageInfo;
        };
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
